﻿//	Copyright (c) 2013 Jonathan Loe
//
//	MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;

using Ordinaire.Data;

namespace Ordinaire.Commerce.Data
{
    /// <summary>
    /// Represents table 'CompositeGoods' in the underlying database.
    /// </summary>
    [Table]
    public class CompositeGoods : HierarchyData<int, string>
    {
        #region Variables

        /// <summary>
        /// Storage for association 'FK_CompositeGoods_Goods_Id'.
        /// </summary>
        private EntityRef<Goods> goods = new EntityRef<Goods>();

        /// <summary>
        /// Storage for association 'FK_CompositeGoods_Goods_ParentId'.
        /// </summary>
        private EntityRef<Goods> parentGoods = new EntityRef<Goods>();

        #endregion

        #region Public methods

        /// <summary>
        /// Get or set the row id.
        /// </summary>
        [Column(Name = "Id", DbType = "int IDENTITY NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
        public int RowId { get; set; }

        /// <summary>
        /// Gets or sets the value of column 'GoodId'.
        /// </summary>
        [Column(Name = "GoodId", IsPrimaryKey = true, UpdateCheck = UpdateCheck.Never)]
        public override int Id
        {
            get { return base.Id; }
            set { base.Id = value; }
        }

        /// <summary>
        /// Gets or sets value for column 'ParentGoodId'.
        /// </summary>
        [Column(Name = "ParentGoodId", IsPrimaryKey = true, UpdateCheck = UpdateCheck.Never)]
        public override int ParentId
        {
            get { return base.ParentId; }
            set { base.ParentId = value; }
        }

        /// <summary>
        /// Gets or sets the quantity of good.
        /// </summary>
        [Column(DbType = "decimal(10, 5) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public decimal Quantity { get; set; }

        /// <summary>
        /// Get or set the username who created this entry.
        /// </summary>
        [Column(DbType = "nvarchar(10) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public override string CreatedBy
        {
            get { return base.CreatedBy; }
            set { base.CreatedBy = value; }
        }

        /// <summary>
        /// Get or set the date when the entry was first created.
        /// </summary>
        [Column(DbType = "datetime", UpdateCheck = UpdateCheck.Never)]
        public override DateTime? CreateDate
        {
            get { return base.CreateDate; }
            set { base.CreateDate = value; }
        }

        /// <summary>
        /// Get or set the username who last updated this entry.
        /// </summary>
        [Column(DbType = "nvarchar(10)", UpdateCheck = UpdateCheck.Never)]
        public override string UpdatedBy
        {
            get { return base.UpdatedBy; }
            set { base.UpdatedBy = value; }
        }

        /// <summary>
        /// Get or set the date when the entry was last updated.
        /// </summary>
        [Column(DbType = "datetime")]
        public override DateTime? UpdateDate
        {
            get { return base.UpdateDate; }
            set { base.UpdateDate = value; }
        }

        #endregion

        #region Association

        /// <summary>
        /// Gets or sets the associated data from table 'Goods' against column 'Id'.
        /// </summary>
        [Association(Name = "FK_CompositeGoods_Goods_Id", IsForeignKey = true, ThisKey = "Id", Storage = "goods")]
        public Goods Goods
        {
            get { return this.goods.Entity; }
            set { this.goods.Entity = value; }
        }

        /// <summary>
        /// Gets or sets the associated data from table 'Goods' against column 'ParentId'.
        /// </summary>
        [Association(Name = "FK_CompositeGoods_Goods_ParentId", IsForeignKey = true, ThisKey = "ParentId", Storage = "parentGoods")]
        public Goods ParentGoods
        {
            get { return this.parentGoods.Entity; }
            set { this.parentGoods.Entity = value; }
        }

        #endregion
    }

    /// <summary>
    /// Contains list of extension methods for Ordinaire.Commerce.Data.CompositeGoods.
    /// </summary>
    public static class CompositeGoodsExtensions
    {
        /// <summary>
        /// Converts the given data to Ordinaire.Commerce.GoodComposition.
        /// </summary>
        /// <param name="data">the data to convert</param>
        /// <returns>the instance of Ordinaire.Commerce.GoodComposition</returns>
        public static GoodComposition ToGoodComposition(this CompositeGoods data)
        {
            if (data == null)
            {
                return null;
            }

            GoodComposition composition = data.Goods.ToGood<GoodComposition>();
            composition.Quantity = data.Quantity;

            return composition;
        }
    }
}
